package rabbitmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class ProducerDemo {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 1. 建立连接
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("134.175.180.147");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("study");
        connectionFactory.setPassword("study");
        connectionFactory.setVirtualHost("bite");
        Connection connection = connectionFactory.newConnection();
        // 2. 开启信道
        Channel channel = connection.createChannel();
        // 3. 声明交换机
        // 4. 声明队列
        channel.queueDeclare("hello", true, false, false, null);
        // 5. 发送消息
        String msg = "String rabbitmq~";
        channel.basicPublish("", "hello", null, msg.getBytes());
        System.out.println("消息发送成功!");
        // 6. 资源释放
        channel.close();
        connection.close();
    }
}
